package com.gatesim;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.Log;

public class OrGate extends Gate{
	
	private static int initResources = 0;
	private static int ORGATE_TOPPIN;
	private static int ORGATE_BOTPIN;
	private static int ORGATE_FNTPIN;
    private Bitmap picToDraw;
    private Paint paint;
	private static int or_width;
	private static int or_height;
	
    protected static Bitmap defaultPic;
    protected static Bitmap selectedPic;
    protected static Bitmap chargedPic;
	
	public OrGate(Point p){
		super(p);
		
		if(initResources == 0){
			initOr();
			initResources++;
		}	
		corner.set( center.x - (or_width/2), center.y - (or_height/2) );
		calcPins();
		paint = new Paint();
        picToDraw = defaultPic;
        width = or_width;
        height = or_height;
	}
	
	private static void initOr(){
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inTempStorage = new byte[16*1024];
        
        //Load image, 1x
        // Determine whether to use cellphone version of non cellphone
        if ( false ){
            defaultPic  =  BitmapFactory.decodeResource(resources, R.drawable.or, options);
            selectedPic = BitmapFactory.decodeResource(resources, R.drawable.selectedor, options);
            chargedPic  =   BitmapFactory.decodeResource(resources, R.drawable.or_charged, options);
        }else{
            defaultPic =         BitmapFactory.decodeResource(resources, R.drawable.or_cp, options);
            selectedPic = BitmapFactory.decodeResource(resources, R.drawable.selectedor_cp, options);
            chargedPic=  BitmapFactory.decodeResource(resources, R.drawable.or_charged_cp, options);
        }
	    
		//Set Constants
		ORGATE_TOPPIN = defaultPic.getHeight()/5;
		ORGATE_BOTPIN = defaultPic.getHeight()*4/5;
		ORGATE_FNTPIN = defaultPic.getHeight()/2;
		or_width = defaultPic.getWidth();
		or_height = defaultPic.getHeight();

	}
	
	private void calcPins(){
		topPin = new InputConnection(new Point(corner.x, corner.y + ORGATE_TOPPIN));
		botPin = new InputConnection(new Point(corner.x,corner.y + ORGATE_BOTPIN));
		fntPin = new OutputConnection(new Point(corner.x + or_width,corner.y + ORGATE_FNTPIN));
	}	
	
	public void recalcPins(){
		topPin.setPoint(corner.x, corner.y + ORGATE_TOPPIN);
		botPin.setPoint(corner.x,corner.y + ORGATE_BOTPIN);
		fntPin.setPoint(corner.x + or_width,corner.y + ORGATE_FNTPIN);
	}
	
	/***
	 * Not used yet
	 */
	@Override
	public int signalOut(){
	    return 0;
	}
	
	@Override //OVERHAUL
	public int produceOut(){
		// Or its parents together
		int out = 0;
		
		for(Wire w: parents){
			if(w == null){
				fntPin.setCharge(Connection.CONN_NO_CHARGE);
				break;
			} else {
				w.produceOut(); //.getGateSource().produceOut();
				w.relayCharge();
				
                if (GateWorld.animationMode && (this.personalDelay < (w.personalDelay + this.delayValue))){
                    personalDelay = new Integer(w.personalDelay + this.delayValue);
                }
				
				if(w.getDest().getCharge() == Connection.CONN_NO_CHARGE || 
						out == Connection.CONN_NO_CHARGE){
					out = Connection.CONN_NO_CHARGE;
				} else {
					out = out | w.getDest().getCharge();
				}
				fntPin.setCharge(out);
			}
		}
        
//        personalDelay = new Integer(personalDelay.intValue() + delayValue);
//	    i = new Integer(i.intValue() + delayValue);
//        if ( personalDelay.intValue() < i.intValue() ){
//            personalDelay = new Integer(i.intValue());
//      }
	    
//		if(GateWorld.animationMode){
			if (_gCount == personalDelay.intValue()) { //i.intValue() ){
			    Log.d("AnimateTimer", "ORGATE: _gCount: " + _gCount);
	    		if(fntPin.getCharge() == Connection.CONN_ONE_CHARGE){
	    			picToDraw = chargedPic;
	    		}else{
	    		    picToDraw = defaultPic;
	    		}
	    		_gCount = 0;
			}
	        _gCount++;
//		}



		return fntPin.getCharge();
	}
	
	public void endSimulationMode(){
		picToDraw = defaultPic;
		_gCount = 0;
        personalDelay = new Integer(0);
	}
	
	@Override
	public void draw(Canvas c, Resources r, boolean simulationMode) {				
		c.drawBitmap(picToDraw, corner.x, corner.y, null);
		fntPin.draw(c, r);
		topPin.draw(c, r);
		botPin.draw(c, r);
		if(simulationMode) {
			paint.setTextSize(50);
			paint.setColor(Color.RED);
			c.drawText(""+ ((fntPin.getCharge() == Connection.CONN_NO_CHARGE) ? "X" : fntPin.getCharge()), 
					fntPin.getPoint().x + 20, fntPin.getPoint().y, paint);
		}
	}
    @Override	
	public int getWidth(){
		return or_width;
	}
    @Override
	public int getHeight(){
		return or_height;
	}
    
    @Override
    public void select(boolean b, boolean simulationMode){
    	if(!simulationMode){
    		picToDraw = (b) ? selectedPic : defaultPic; 
    	}
    }
}
